package io.xxx.omni.oss.sdk

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.toJsonObject
import io.xxx.omni.oss.common.toJsonString
import io.xxx.omni.oss.domain.Store
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
import org.springframework.web.util.UriComponentsBuilder

@Component
class HzwClient {

    @Autowired
    private lateinit var restTemplate: RestTemplate

    private val httpUrl = "http://dapopen.haiziwang.com/pop/"

    fun execute(store: Store, path: String, request: Map<String, Any>): JsonObject {
        val authorization = store.authorization
        val url = UriComponentsBuilder
            .fromHttpUrl(httpUrl)
            .path(path)
            .queryParam("appkey", authorization["appkey"])
            .queryParam("loginName", authorization["loginName"])
            .queryParam("jsonStr", request.toJsonString())
            .toUriString()
        return restTemplate.getForEntity(url, String::class.java).body!!.toJsonObject()
    }
}